import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cartItems:JSON.parse(localStorage.getItem('cart')) || [],
    allSelect:JSON.parse(localStorage.getItem('allSelect')) || false,
    totalPrice:JSON.parse(localStorage.getItem('totalPrice')) || 0,
  },
  mutations: {
    ADD_CART(state,item){
      var flag = false
      state.cartItems.forEach((ele)=>{
        if(ele.id === item.id){
          flag = true
          ele.count ++
        }
      })
      if(!flag){
        item.checked = false
        item.count = 1
        state.cartItems.push(item)
      }
      
      localStorage.setItem("cart",JSON.stringify(state.cartItems))
    },
    CHANGE_CHECKED(state,item){
      state.cartItems.forEach((ele)=>{
        if(item.id === ele.id){
          ele.checked = item.checked;
        }
      })

      state.allSelect  = state.cartItems.every(ele => {
        return ele.checked === true;
      });
    
      state.cartItems = [...state.cartItems]
      // 计算总商品价
      state.totalPrice = 0
      state.cartItems.forEach((ele)=>{
        if(ele.checked){
          state.totalPrice += ele.count * ele.price
        }
      })
      localStorage.setItem("totalPrice",JSON.stringify(state.totalPrice))
      localStorage.setItem("cart",JSON.stringify(state.cartItems))
      localStorage.setItem("allSelect",JSON.stringify(state.allSelect))
    },
    ALL_CHECKED(state,boolean){
      state.allSelect = boolean
      
      state.cartItems.forEach((element) => {
        element.checked = boolean;
      });

      // 计算商品总价
      state.cartItems = [...state.cartItems]
      state.totalPrice = 0
      state.cartItems.forEach((ele)=>{
        if(ele.checked){
          state.totalPrice += ele.count * ele.price
        }
      })
      localStorage.setItem("totalPrice",JSON.stringify(state.totalPrice))
      localStorage.setItem("cart",JSON.stringify(state.cartItems))
      localStorage.setItem("allSelect",JSON.stringify(state.allSelect))
    },
    // 数量减少功能
    COUNT_MINUS(state,item){
      state.cartItems.forEach((ele)=>{
        if(ele.id === item.id){
          if(ele.count > 1){
            ele.count--
          }
        }
      })
      // state.cartItems = [...state.cartItems]
      // 计算总价
      state.totalPrice = 0
      state.cartItems.forEach((ele)=>{
        if(ele.checked){
          state.totalPrice += ele.count * ele.price
        }
      })
      localStorage.setItem("totalPrice",JSON.stringify(state.totalPrice))
      localStorage.setItem("cart",JSON.stringify(state.cartItems))
    },
    // 数量增加功能
    COUNT_PLUS(state,item){
      console.log(item)
      state.cartItems.forEach((ele)=>{
        if(ele.id === item.id){
          ele.count++
        }
      })

      state.cartItems = [...state.cartItems]
      // 计算总价
      state.totalPrice = 0
      state.cartItems.forEach((ele)=>{
        if(ele.checked){
          state.totalPrice += ele.count * ele.price
        }
      })
      localStorage.setItem("totalPrice",JSON.stringify(state.totalPrice))
      localStorage.setItem("cart",JSON.stringify(state.cartItems))
    }
  },
  actions: {
    ADD_CART(context,item){
      context.commit('ADD_CART',item)
    },
    CHANGE_CHECKED(context,item){
      context.commit('CHANGE_CHECKED',item)
    },
    ALL_CHECKED(context,boolean){
      context.commit('ALL_CHECKED',boolean)
    },
    COUNT_MINUS(context,item){
      context.commit('COUNT_MINUS',item)
    },
    COUNT_PLUS(context,item){
      context.commit('COUNT_PLUS',item)
    }
  },
  modules: {
  }
})
